/* Copyright (C) 2008 Robin Debreuil -- Released under the BSD License */

using System;
using System.Collections.Generic;
using System.CodeDom.Compiler;


namespace SWF
{
    public class StyleChangedRecord : IRecord
	{
		/*
			STYLECHANGERECORD
			Field				Type					Comment
			TypeFlag			UB[1]					Non-edge record flag. Always 0.
			StateNewStyles		UB[1]					New styles flag. Used by DefineShape2 and DefineShape3 only.
			StateLineStyle		UB[1]					Line style change flag.
			StateFillStyle1		UB[1]					Fill style 1 change flag.
			StateFillStyle0		UB[1]					Fill style 0 change flag.
			StateMoveTo			UB[1]					Move to flag.		 
			MoveBits			If StateMoveTo 			Move bit count.	
									UB[5]	 
			MoveDeltaX			If StateMoveTo			Delta X value.
									SB[MoveBits]			
			MoveDeltaY			If StateMoveTo			Delta Y value.
									SB[MoveBits]			
			FillStyle1			If StateFillStyle1		Fill 1 Style.
									UB[FillBits]			
			FillStyle0			If StateFillStyle0		Fill 0 Style.
									UB[FillBits]			
			LineStyle			If StateLineStyle		Line Style.
									UB[LineBits]			
			FillStyles			If StateNewStyles		Array of new fill styles.
									FILLSTYLEARRAY			
			LineStyles			If StateNewStyles		Array of new line styles.
									LINESTYLEARRAY			
			NumFillBits			If StateNewStyles		Number of fill index bits for new styles.
									UB[4]
			NumLineBits			If StateNewStyles		Number of line index bits for new styles.
									UB[4]		
		 */

		public int  MoveDeltaX;
		public int  MoveDeltaY;
		public uint FillStyle1;
		public uint FillStyle0;
		public uint LineStyle;
		public FillStyleArray FillStyles;
		public LineStyleArray LineStyles;
		public bool HasMove;
		public bool HasFillStyle0;
		public bool HasFillStyle1;
		public bool HasLineStyle;
		public bool HasNewStyles;

		public StyleChangedRecord(SwfReader r, uint flags, ref uint fillBits, ref uint lineBits, ShapeType shapeType)
		{
			MoveDeltaX = 0;
			MoveDeltaY = 0;
			FillStyle1 = 0;
			FillStyle0 = 0;
			LineStyle = 0;

            HasNewStyles    = (flags & 0x10) != 0;
            HasLineStyle    = (flags & 0x08) != 0;
            HasFillStyle1   = (flags & 0x04) != 0;
            HasFillStyle0   = (flags & 0x02) != 0;
            HasMove         = (flags & 0x01) != 0;

            if (shapeType == ShapeType.Glyph) // glyphs really should be a different style changed record imo : )
            {                
                if (HasMove)
                {
                    uint moveBits = r.GetBits(5);
                    this.MoveDeltaX = r.GetSignedNBits(moveBits);
                    this.MoveDeltaY = r.GetSignedNBits(moveBits);
                }

                if (HasFillStyle0)
                {
                    this.FillStyle0 = r.GetBits(fillBits);
                }
            }
            else
            {
                if (HasMove)
                {
                    uint moveBits = r.GetBits(5);
                    this.MoveDeltaX = r.GetSignedNBits(moveBits);
                    this.MoveDeltaY = r.GetSignedNBits(moveBits);
                }
                if (HasFillStyle0)
                {
                    this.FillStyle0 = r.GetBits(fillBits);
                }

                if (HasFillStyle1)
                {
                    this.FillStyle1 = r.GetBits(fillBits);
                }

                if (HasLineStyle)
                {
                    this.LineStyle = r.GetBits(lineBits);
                }

                //r.Align();

                if (HasNewStyles)
                {
                    FillStyles = new FillStyleArray(r, shapeType);
                    LineStyles = new LineStyleArray(r, shapeType);
                    fillBits = r.GetBits(4);
                    lineBits = r.GetBits(4);
                }
                else
                {
                    FillStyles = new FillStyleArray();
                    LineStyles = new LineStyleArray();
                    HasNewStyles = false;
                }
            }
		}
	}
}
